﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;

public class _14 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        UnityEngine.Debug.Log("浅拷贝");
        Employee mike = new Employee() { IDCode = "NB123", Age = 30, Department = new Department() { Name = "游戏一世1" } };
        Employee rose = mike.Clone() as Employee;
        UnityEngine.Debug.Log(rose.IDCode);
        UnityEngine.Debug.Log(rose.Age);
        UnityEngine.Debug.Log(rose.Department);
        UnityEngine.Debug.Log("开始改变值");
        mike.IDCode = "NB456";
        mike.Age = 60;
        mike.Department.Name = "游戏一世2";
        UnityEngine.Debug.Log(rose.IDCode);
        UnityEngine.Debug.Log(rose.Age);
        UnityEngine.Debug.Log(rose.Department);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
[Serializable]
public class Department
{
    public string Name{ get; set; }
    public override string ToString()
    {
        return this.Name;
    }
}

[Serializable]
public class Employee : ICloneable
{
    public string IDCode{ get; set; }
    public int Age{ get; set; }
    public Department Department { get; set; }
    public object Clone()
    {
        //浅拷贝
        return this.MemberwiseClone();

        //深拷贝
        //using (Stream objectStream = new MemoryStream())
        //{
        //    IFormatter formatter = new BinaryFormatter();
        //    formatter.Serialize(objectStream, this);
        //    objectStream.Seek(0, SeekOrigin.Begin);
        //    return formatter.Deserialize(objectStream) as Employee;
        //}
    }

    public Employee DeepClone()
    {
        using (Stream objectStream = new MemoryStream())
        {
            IFormatter formatter = new BinaryFormatter();
            formatter.Serialize(objectStream, this);
            objectStream.Seek(0, SeekOrigin.Begin);
            return formatter.Deserialize(objectStream) as Employee;
        }
    }
}

